package com.bennu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bennu.common.BaseContext;
import com.bennu.common.R;
import com.bennu.domain.OrderDetail;
import com.bennu.domain.Orders;
import com.bennu.service.OrderDetailService;
import com.bennu.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;
    //用户下单
    @PostMapping("/submit")
    public R<String> submit( @RequestBody Orders orders){
        log.info( orders.toString() );
        orderService.submit( orders );
        return R.success( "下单成功" );
    }
    //后台管理订单明细
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize ,String number, String beginTime, String endTime){
        //分页条件构造器
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        //构造条件
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(number != null,Orders::getId, number);
        queryWrapper.ge(beginTime != null,Orders::getOrderTime,beginTime);
        queryWrapper.le(endTime != null,Orders::getOrderTime,endTime);
        //添加排序规则
        queryWrapper.orderByDesc( Orders::getCheckoutTime );
        //执行查询条件
        orderService.page( pageInfo,queryWrapper );
        List<Orders> records = pageInfo.getRecords();
        records = records.stream().map((item) -> {

            item.setUserName("用户" + item.getUserId());
            return item;
        }).collect( Collectors.toList());

        return R.success( pageInfo );
    }
    //派送订单
    @PutMapping
    public R<String> updateOrder(@RequestBody Orders orders){
        //条件构造器
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.eq( Orders::getId,orders.getId() );
        orderService.updateById(orders);
        return R.success( "修改成功" );
    }
    //历史订单
    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize , String beginTime, String endTime){
        //分页条件构造器
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        //获取当前用户id
        Long currentId = BaseContext.getCurrentId();
        //构造条件
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq( Orders::getUserId,currentId );
        queryWrapper.ge(beginTime != null,Orders::getOrderTime,beginTime);
        queryWrapper.le(endTime != null,Orders::getOrderTime,endTime);
        //添加排序规则
        queryWrapper.orderByDesc( Orders::getCheckoutTime );
        //执行查询条件
        orderService.page( pageInfo,queryWrapper );
        return R.success( pageInfo );
    }

}
